package com.cqut.forum.service.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.cqut.forum.dao.ArticleDao;
import com.cqut.forum.dao.ModuleDao;
import com.cqut.forum.dao.PictureDao;
import com.cqut.forum.dao.PictureGroupDao;
import com.cqut.forum.dao.RoleDao;
import com.cqut.forum.dao.UserDao;
import com.cqut.forum.dao.WallDao;
import com.cqut.forum.entity.Picture;
import com.cqut.forum.entity.PictureGroup;
import com.cqut.forum.entity.User;
import com.cqut.forum.service.ControlService;

@Component
public class ControlServiceImp implements ControlService {
	@Resource
	private UserDao userDao;
	@Resource
	private RoleDao roleDao;
	@Resource
	private ArticleDao articleDao;
	@Resource
	private ModuleDao moduleDao;
	@Resource
	private WallDao wallDao;
	@Resource
	private PictureDao pictureDao;
	@Resource
	private PictureGroupDao pgDao;
	@SuppressWarnings("deprecation")
	@Override
	public List<Map<String, Object>> loadUser(String userName,
			String userPlace, String registerTime, int index, int size) {
		List<Map<String, Object>> users = userDao.getUsers(userName, userPlace, registerTime, index, size);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		for(Map<String, Object> u:users){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", u.get("user_id"));
			map.put("userName", u.get("user_name"));
			map.put("userSex", u.get("user_sex"));
			map.put("userPlace", u.get("user_place"));
			int nowYear = new Date().getYear();
			map.put("userAge", u.get("user_birthday")==null?"":nowYear-((Date)u.get("user_birthday")).getYear());
			map.put("registerTime", ((Date)u.get("user_time")).toLocaleString());
			map.put("lastLoginTime", ((Date)u.get("user_last_login_time")).toLocaleString());
			map.put("userRole", u.get("role_name"));
			map.put("userState", ((Boolean)u.get("user_state"))?"正常":"停用");
			
			result.add(map);
		}
		return result;
	}
	@Override
	public boolean updateUserRole(List<Integer> users,List<String> roles) {
		int length = users.size();
		List<Map<String, Object>> role = roleDao.getAllRole();
		for(int i =0;i<length;i++){
			if(roles.get(i).equals("停用")){
				userDao.unDelete(users.get(i));
			}
			else if(roles.get(i).equals("启用")){
				User user = userDao.get(users.get(i), User.class);
				user.setUserState(true);
				userDao.update(user);
			}
			else{
				for(Map<String, Object> r:role){
					if(roles.get(i).equals(r.get("role_name"))){
						userDao.updateUserRole(users.get(i),(Integer)r.get("role_id"));
						break;
					}
				}
			}
		}
		return true;
	}
	@SuppressWarnings("deprecation")		
	@Override
	public List<Map<String, Object>> loadArticle(String articleOwner,
			String articleTitle, String publishTime, int index, int size) {
		int moduleId = moduleDao.getModuleIndex("博客");
		List<Map<String, Object>> articles = articleDao.getArticles(articleOwner,articleTitle,publishTime,moduleId, index, size);
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		for(Map<String, Object> a:articles){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("articleId", a.get("article_id"));
			map.put("articleOwner", a.get("user_name"));
			map.put("articleTitle", a.get("article_name"));
			map.put("articleType", a.get("article_group_name"));
			map.put("publishTime", ((Date)a.get("publish_time")).toLocaleString());
			map.put("articleState", ((Boolean)a.get("article_state"))?"正常":"删除");
			
			result.add(map);
		}
		return result;
	}
	@Override
	public boolean updateArticleState(int userId,List<Integer> articles, List<String> states) {
		int length = articles.size();
		for(int i =0;i<length;i++){
			articleDao.updateArticleState(userId,articles.get(i),states.get(i).equals("删除")?false:true);
		}
		return true;
	}
	@Override
	public boolean updateWallState(List<Integer> wallS, List<String> states) {
		int length = wallS.size();
		for(int i =0;i<length;i++){
			wallDao.updateWallState(wallS.get(i),states.get(i).equals("删除")?false:true);
		}
		return true;
	}
	@Override
	public boolean pictureTypeHandle(User user,List<Integer> pictures, List<String> types) {
		int length = pictures.size();
		for(int i =0;i<length;i++){
			Picture p = pictureDao.get(pictures.get(i), Picture.class);
			PictureGroup pg = pgDao.getByName(types.get(i));
			p.setPictureGroup(pg);
			p.setOwnerId(user.getUserId());
			p.setPictureState(true);
			p.setReviewTimes(0);
			p.setVisitTimes(0);
			p.setUploadTime(new Date());
			pictureDao.save(p);
		}
		return true;
	}
	@Override
	public int countSize(String moduleName) {
		int moduleId = 0;
		int size = 0;
		if(moduleName.equals("article")){
			moduleId = moduleDao.getModuleIndex("博客");
			size = articleDao.count(moduleId);
		}
		else if(moduleName.equals("picture")){
			size = pictureDao.count(true);
			System.out.println(size);
		}
		else if(moduleName.equals("garden")){
			moduleId = moduleDao.getModuleIndex("花园");
			size = articleDao.count(moduleId);
		}
		else if(moduleName.equals("dongtai")){
			moduleId = moduleDao.getModuleIndex("动态");
			size = articleDao.count(moduleId);
		}
		return size;
	}
}
